<?php

namespace app\index\controller;
session_start();

use think\Controller;
use think\Request;
use think\Session;
use think\Db;

class Login extends Controller
{
    /**
     * 显示资源列表
     *
     * @return \think\Response
     */
    public function index()
    {
        return view('Login/login');
    }

    public function dologin(Request $request)
    {
        $rel = $request->post();
        $username = $rel['username'];

        $data = Db::table('l_user')
                ->where('name', $username)
                ->whereor('tel', $username)
                ->find();

        if(!$data){
            $this->error('用户名或手机号不存在');
        }
        if($data['password'] != md5($rel['password'])){
            $this->error('密码错误');
        }

        $_SESSION['home'] = $data;

        $this->redirect('index/Index/index');
    }

    public function pass()
    {
        return view('Login/pass');
    }

    public function register()
    {
        return view('Login/register');
    }

    public function doregister()
    {
        $request = Request::instance();
        $rel = $request->post();
        if(!$rel['name']){
            $this->error('用户名不能为空');
            exit;
        }
        if(!$rel['tel']){
            $this->error('手机号不能为空');
            exit;
        }

        if($rel['password'] != $rel['repassword']){
            $this->error('两次密码不一致');
            exit;
        }

        $data = [
            'name'=>$rel['name'],
            'tel'=>$rel['tel'],
            'password'=>md5($rel['password']),
            'time'=>time()
        ];

        $list = Db::table('l_user')
                ->insert($data);

        if($list){
            $this->success('注册成功', 'index/Login/index');
        }

    }


    /**
     * 删除指定资源
     *
     * @param  int  $id
     * @return \think\Response
     */
    public function nologin()
    {
        unset($_SESSION['home']);
        $this->redirect('index/Login/index');
    }
}
